package org.ilead.iserve.web;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.ilead.iserve.constants.SessionKeyConstant;
import org.ilead.iserve.model.PrivateMessage;
import org.ilead.iserve.model.User;
import org.ilead.iserve.model.base.PageView;
import org.ilead.iserve.service.IPrivateMessagePageService;
import org.ilead.iserve.service.IPrivateMessageService;
import org.ilead.iserve.service.manager.AreaManager;
import org.ilead.iserve.service.manager.MessageManager;
import org.ilead.iserve.service.manager.UserManager;
import org.ilead.iserve.web.base.BaseActionSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;

public class PrivateMessageAction extends BaseActionSupport {
	private static Logger logger = LoggerFactory.getLogger(PrivateMessageAction.class);
	private PrivateMessage m = new PrivateMessage();
	
	private IPrivateMessageService privateMessageService;
	@Autowired
	private IPrivateMessagePageService privateMessagePageService;
	
	public void setPrivateMessageService(
			IPrivateMessageService privateMessageService) {
		this.privateMessageService = privateMessageService;
	}

	public String listPrivateMessage(){
		User user = UserManager.getInstance().getLoginUser();
		int userId = user.getId();
		String where = "o.target=" + userId + " and o.reState!=-1";
		String result = listMessageByCondition(where);
		//将显示的列表全部标为已读
		MessageManager.getInstance().readListPrivateMessage();
		return result;
	}
	
	public String listSendPrivateMessage(){
		User user = UserManager.getInstance().getLoginUser();
		int userId = user.getId();
		String where = "o.source=" + userId + " and o.seState=0";
		return listMessageByCondition(where);
	}
	
	public String sendPrivateMessagePage(){
		Map<String, Object> sessionMap = ActionContext.getContext().getSession();
		sessionMap.put(SessionKeyConstant.REGISTER_USER_LIST, UserManager.getInstance().getAllRegisterUser());
		return SUCCESS;
	}
	
	public String sendPrivateMessage(){
		m.setSendTime(new Date());
		m.setSeState(0);
		m.setReState(0);
		try{
			privateMessageService.addMessage(m);
			return SUCCESS;
		}catch(Exception e){
			logger.error("发送私信失败，原因：\n" + e);
			return ERROR;
		}
	}
	
	public PrivateMessage getM() {
		return m;
	}

	public void setM(PrivateMessage m) {
		this.m = m;
	}
	
	public List<User> listMessageTarget(){
		return UserManager.getInstance().getAllRegisterUser();
	}
	
	private String listMessageByCondition(String where){
		/**
		 * 下面3句固定
		 */
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		int maxResult = 20;
		/**
		 * 修改Bean和service即可
		 */
		PageView<PrivateMessage> pageView = new PageView<PrivateMessage>(maxResult, getPage());
		LinkedHashMap<String, String> orderMap = new LinkedHashMap<String, String>();
		orderMap.put("sendTime", "desc");
		
		pageView.setQueryResult(privateMessagePageService.getScrollData(pageView.getFirstResult(), maxResult, where,null, orderMap));
		
		/**
		 * request.setAttribute("pageView", pageView)中key尽量为pageView，不然需要修改代码
		 */
		request.setAttribute("pageView", pageView);
		MessageManager.getInstance().setPrivateMessageList(pageView.getRecords());
		return SUCCESS;
	}
	
}
